<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <meta charset="UTF-8">
    <title>过滤相似单词</title>
    <style type="text/css">

    </style>
    <script type="text/javascript">
        "use strict";

        function load() {

        }
        var arrOldWord = new Array;
        var arrNewWord = new Array;
        var arrTotal = new Array;
        var sameShort = new Array;
        var sameLong = new Array;

        function init() {
            var text01 = document.getElementById("area01").value;
            arrOldWord = text01.split("\n");
            var text02 = document.getElementById("area02").value;
            arrNewWord = text02.split("\n");
            //去除数组中的空项
            for (var i = arrNewWord.length; i > -1; i--) {
                if (arrNewWord[i] == "") {
                    arrNewWord.splice(i, 1);
                }
            }
            for (var i = arrOldWord.length; i > -1; i--) {
                if (arrOldWord[i] == "") {
                    arrOldWord.splice(i, 1);
                }
            }
            //检查内部是否有重复项
            for (var i = arrNewWord.length; i > 1; i--) {
                for (var j = i - 1; j > -1; j--) {
                    if (arrNewWord[i] == arrNewWord[j]) {
                        arrNewWord.splice(i, 1);
                        break;
                    }
                }
            }

            //检查两个数组是否有重复项，如果有，则删除arrNewWord的内容
            for (var i = arrNewWord.length - 1; i > -1; i--) {
                if (arrOldWord.indexOf(arrNewWord[i]) > -1) {
                    arrNewWord.splice(i, 1);
                }
            }
        }

        function btnok() {

            init();
            //检查是否有包含关系
            isSame();
            //去除相似词条检查中，完全相同的结果
            delSame();

            console.log(sameShort);
            console.log(sameLong);

        }
        //去除相似词条检查中，完全相同的结果
        function delSame(){
            for(var i=sameShort.length-1;i>-1;i--){
              if(sameLong[i]==sameShort[i]){
                  sameLong.splice(i,1);
                  sameShort.splice(i,1);
              }
            }
            console.log(sameShort);
            console.log(sameLong);
            for(var i=sameShort.length-1;i>0;i--){
                for(var j=0;j<i;j++){
                    if((sameShort[j]==sameShort[i])&(sameLong[j]==sameLong[i])){
                       sameShort.splice(i,1);
                       sameLong.splice(i,1);
                        //break;
                    }
                }
            }

        }

        //是否存在完全相同的单词
        function isSame() {

            var shortW = "";
            var longW = "";
            arrTotal = arrOldWord.concat(arrNewWord);
            for (var i = 1; i < arrTotal.length; i++) {
                for (var j = 0; j < i; j++) {
                    longW = "";
                    shortW = "";
                    if (arrTotal[i].length > arrTotal[j].length) {
                        longW = arrTotal[i];
                        shortW = arrTotal[j];
                    } else {
                        longW = arrTotal[j];
                        shortW = arrTotal[i];
                    }
                    // 确定了包含关系,p表示单词判断的最短长度，太短则没有判断的意义
                    // 例如of会包含在多个单词中
                    var p = 4;
                    if (shortW.length > p & longW.includes(shortW)) {
                        var tabSame = document.getElementById("tabSame");
                        var row = tabSame.insertRow(tabSame.rows.length);
                        var cell1 = row.insertCell(0);
                        var cell2 = row.insertCell(1);
                        var cell3 = row.insertCell(2);
                        cell1.innerHTML = shortW;
                        cell2.innerHTML = longW;
                        sameShort.push(shortW);
                        sameLong.push(longW);
                    }
                    // q是判断相似基本单元的长度
                    var q = 5;
                    if (shortW.length > q) {
                        isSimilar(shortW, longW, q);
                    }
                }
            }

        }
        // 检查是否有相似关系，把短的单词截取指定长度的字符，看长单词是否包含
        //str1是较短的单词，str2是较长的单词
        function isSimilar(str1, str2, l) {
            for (var k = 0; k < (str1.length - l); k++) {
                var subW = str1.substring(k, k + l);
                if (str2.includes(subW)) {
                    var tabSimilar = document.getElementById("tabSimilar");
                    var row = tabSimilar.insertRow(tabSimilar.rows.length);
                    var cell1 = row.insertCell(0);
                    var cell2 = row.insertCell(1);
                    var cell3 = row.insertCell(2);
                    cell1.innerHTML = str1;
                    cell2.innerHTML = str2;
                    sameShort.push(str1);
                    sameLong.push(str2);
                }

            }
        }

        function testcode() {
            var str1 = "thisisa";
            var str2 = "thatthisisaapple";
            var l = 5;
            for (var k = 0; k < (str1.length - l); k++) {
                var subW = str1.substring(k, k + l);
                if (str2.includes(subW)) {
                    var tabSimilar = document.getElementById("tabSimilar");
                    var row = tabSimilar.insertRow(tabSame.rows.length);
                    var cell1 = row.insertCell(0);
                    var cell2 = row.insertCell(1);
                    var cell3 = row.insertCell(2);
                    cell1.innerHTML = str1;
                    cell2.innerHTML = str2;
                }
                console.log("k=" + k);
                console.log("subW=" + subW);

            }
        }
    </script>
</head>

<body onload="load()">
    <input type="button" onclick="testcode()" value="test">
    <div>
        <p>输入的词汇</p>
        <textarea id="area01" warp="virtual"></textarea>
    </div>
    <div>
        <p>词汇表</p>
        <textarea id="area02" warp="virtual"></textarea>
    </div>
    <div>
        <p>抛弃的字符</p>
        <textarea id="area03" warp="virtual"></textarea>
    </div>
    <div>
        <input type="button" onclick="btnok()" value="ok">
        <input type="button" onclick="cleartxt()" value="clear">
    </div>
    <div>
        <table id="tabSame" border="1" cellspacing="0">
            <tr>
                <td class="num">序号</td>
                <td class="timu">题目</td>
                <td class="daan">答案</td>

            </tr>
        </table>
    </div>
    <div>
        <table id="tabSimilar" border="1" cellspacing="0">
            <tr>
                <td class="num">序号</td>
                <td class="timu">题目</td>
                <td class="daan">答案</td>

            </tr>
        </table>
    </div>



</body>

</html>